
# sh.fig-- copies output of shell commands into text buffer
# Copyright (C) 2021 mn
#
# This program is free software: you can redistribute it and/or modify 
#
# it under the terms of the GNU Affero General Public License as published by 
# the Free Software Foundation, either version 3 of the License, or 
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License 
# along with this program. If not, see <http://www.gnu.org/licenses/>.

function vget g k
python
    return g[k]
    fig
    fig

function vset g k v
python
    g[k] = v
    return g
    fig
    fig

function sh buf params
    insp    vget params "insp"
    glen    vget params "g" ltrim len
    tlen    ".sh"    len    times -1  plus glen
    cmdline vget params "g"        ltrim       right tlen  ltrim
    cmdlinenblen cmdline rtrim len          minus 1
    cmdlinenb    cmdline left  cmdlinenblen
python
    cmdline = cmdline.encode('utf-8')
    cmdlinenb = cmdlinenb.encode('utf-8')
    fig
    pad arr mid 1 0
    pad vget params "g"  arr
    bg  vget params "g"       rtrim  right 1   instr bg "&"
    fg  vget params "g"       rtrim  right 1   instr fg "!"
    iftrue fg
        now cmdlinenb shell
        now vset params "pad" pad
        params now return params
        next
    ifequal bg 0
        padcmd cmdline arrshell
        pad plus padcmd
        next
    iftrue bg
        now cmdline shell
        next
    iftrue pad
        now vset params "text" buf  vset params "pad" pad
        params now return params
        next
    fig

